package prickworlds.game;

import static org.lwjgl.opengl.GL11.glColor3f;
import prickworlds.Prickworlds;
import prickworlds.graphics.FullTexture;
import prickworlds.graphics.Texture;
import prickworlds.physics.RectRigidBody;

public class DropItem extends SolidObject {

	public static class DropTemplate {
		public enum Type {
			RELOAD_UP, LIFE_UP, HEAL_LIFE, DAMAGE_UP
		};

		protected Type type;
		protected FullTexture dropTexture;

		public DropTemplate(Type type) {
			this.type = type;
			if (type == Type.HEAL_LIFE) {
				dropTexture = FullTexture.loadImage(Prickworlds.class
						.getResourceAsStream("data/Health.png"));
			} else {
				dropTexture = FullTexture.loadImage(Prickworlds.class
						.getResourceAsStream("data/DropIcon.png"));
			}
		}

		public Texture getTexture() {
			return dropTexture;
		}

		public Type getType() {
			return type;
		}
	}

	DropTemplate dropType;
	int sizeX;
	int sizeY;

	private float sizeangle;

	boolean finished;

	public DropItem(float posX, float posY, DropTemplate dropType) {
		super(posX, posY);

		this.dropType = dropType;

		sizeX = sizeY = 1;

		finished = false;

		posX -= sizeX / 2;
		posY -= sizeY / 2;
		this.dropType = dropType;
		setHitbox(new RectRigidBody(0, 0, sizeX, sizeY));

		sizeangle = 0.0f;
	}

	@Override
	public void update() {
		sizeangle += 0.1f;
		super.update();
	}

	@Override
	public void display() {
		if (dropType.getType() == DropTemplate.Type.HEAL_LIFE) {
			glColor3f(1f, 1f, 1f);
		} else if (dropType.getType() == DropTemplate.Type.LIFE_UP) {
			glColor3f(0f, 0f, 1f);
		} else if (dropType.getType() == DropTemplate.Type.DAMAGE_UP) {
			glColor3f(1f, 0f, 0f);
		} else if (dropType.getType() == DropTemplate.Type.RELOAD_UP) {
			glColor3f(1f, 1f, 0f);
		}

		Texture dropTexture = dropType.getTexture();

		float size = (float) (1.0f + 0.2 * Math.sin(sizeangle));
		dropTexture.display(posX - size * sizeX / 2, posY - size * sizeY / 2,
				sizeX * size, sizeY * size);
	}

	public void foundBy(Robot robot) {
		if (dropType.getType() == DropTemplate.Type.HEAL_LIFE) {
			robot.restoreFullHealth();
		} else if (dropType.getType() == DropTemplate.Type.LIFE_UP) {
			robot.getTemplate().setMaxHp(robot.getTemplate().getMaxHp() + 100);
		} else if (dropType.getType() == DropTemplate.Type.DAMAGE_UP) {
			robot.activeWeapon.setDamage(robot.activeWeapon.getDamage() + 5);
			robot.activeWeapon
					.setRadius(robot.activeWeapon.getRadius() + 0.05f);
		} else if (dropType.getType() == DropTemplate.Type.RELOAD_UP) {
			// robot.reloadSpeed -= 5;
			robot.addReloadBonus(new Bonus.ReloadSpeedUp(100));
		}

		invalidate();
	}

	public void invalidate() {
		finished = true;
	}

	public boolean isFinished() {
		return finished;
	}
}
